struct TreeNode {
    int val;
    TreeNode* left;
    TreeNode* right;
    TreeNode() : val(0), left(nullptr), right(nullptr) {}
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    TreeNode(int x, TreeNode* left, TreeNode* right) : val(x), left(left), right(right) {}
};

class Solution {
public:
    bool hasPathSum(TreeNode* root, int targetSum) {
        return root == nullptr ? false : has(root, 0, targetSum);
    }

    bool has(TreeNode* node, int cur,int target) {
        bool ret = false;
        cur += node->val;
        if (node->left == nullptr && node->right == nullptr) {
            ret = (cur == target);
        }
        if (!ret&&node->left != nullptr) {
            ret = has(node->left, cur, target);
        }
        if (!ret && node->right != nullptr) {
            ret = has(node->right, cur, target);
        }
        return ret;
    }
};